package im.yisme.model.fileio;

import im.yisme.model.Music;
import im.yisme.model.MusicRepository;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class MusicRepositoryReader {
	
	private String path;
	
	public MusicRepositoryReader(){
		path = DirManager.getRepositoryDir();
	}
	
	public void read(MusicRepository musicRepository) throws IOException{
		ArrayList<Music> list = new ArrayList<Music>();
		musicRepository.setMusicList(list);
		
		File file = new File(path + "music.repo");
		if (!(file.exists() && file.isFile())) return;
		
		BufferedReader reader = new BufferedReader(new FileReader(file));		
		String info;
		while((info = reader.readLine()) != null){
			try {
				String[] datas = info.split("&");
				Music music = new Music(Long.parseLong(datas[0]), datas[1], datas[2], datas[3]);
				music.setFilePath(datas[4]);
				list.add(music);
			} catch (Exception e) {
				// TODO: handle exception
				continue;
			}
		}
		reader.close();
	}
}
